/* @Description:midway-back/src/servicemodel.service.ts @author:378237242@qq.com @date:2023/11/20 */

import { Provide, Config, Inject } from "@midwayjs/core";
import { cloneDeep } from "lodash";
import { excludePrivateProperties } from "../utils/object.utils";
import { hasFalse } from "../utils/array.utils";

@Provide()
export class ModelService {
  @Config("instructionModel")
  instructionModel;

  matchModel(analysisResult: AnalysisResult, strategyArr: string[]) {
    const modules = this.instructionModel;
    if (modules.length > 0) {
      // for (const Model of modules) {
      //   const ModelTemp = cloneDeep(Model.default);
      //   if (
      //     hasFalse(
      //       strategyArr.map((a) => ModelTemp[`_${a}Rule`](analysisResult)),
      //     )
      //   ) {
      //     continue;
      //   } else {
      //     // ModelTemp 命中模型
      //     // 构造模型参数
      //     ModelTemp._paramsStructure(analysisResult);
      //     return excludePrivateProperties(ModelTemp);
      //   }
      // }
      let targetModel = null;
      const ms = modules.filter((Model) => {
        const ModelTemp = Model.default;
        return !hasFalse(
          strategyArr.map((a) => ModelTemp[`_${a}Rule`](analysisResult)),
        );
      });
      if (ms.length > 0) {
        targetModel = cloneDeep(ms[0].default);
        targetModel._paramsStructure(analysisResult);
        return excludePrivateProperties(targetModel);
      } else {
        return null;
      }
    } else {
      throw new Error("the model data is empty");
    }
  }
}
